package com.agw.crm.workbench.service.impl;

import com.agw.crm.utils.DateTimeUtil;
import com.agw.crm.utils.SqlSessionUtil;
import com.agw.crm.utils.UUIDUtil;
import com.agw.crm.workbench.dao.*;
import com.agw.crm.workbench.domain.*;
import com.agw.crm.workbench.service.ClueService;

import java.util.List;

public class ClueServiceIpml implements ClueService {
    //mybatis动态代理getMapper
    private ClueDao clueDao = SqlSessionUtil.getSqlSession().getMapper(ClueDao.class);
    private ClueActivityRelationDao cardDao = SqlSessionUtil.getSqlSession().getMapper(ClueActivityRelationDao.class);
    private ClueRemarkDao clueRemarkDao = SqlSessionUtil.getSqlSession().getMapper(ClueRemarkDao.class);


    //客户模块
    private CustomerDao customerDao = SqlSessionUtil.getSqlSession().getMapper(CustomerDao.class);
    private CustomerRemarkDao customerRemarkDao = SqlSessionUtil.getSqlSession().getMapper(CustomerRemarkDao.class);

    //联系人模块
    private ContactsDao contactsDao = SqlSessionUtil.getSqlSession().getMapper(ContactsDao.class);
    private ContactsRemarkDao contactsRemarkDao = SqlSessionUtil.getSqlSession().getMapper(ContactsRemarkDao.class);
    private ContactsActivityRelationDao contactsActivityRelationDao = SqlSessionUtil.getSqlSession().getMapper(ContactsActivityRelationDao.class);

    //交易模块
    private TranDao tranDao = SqlSessionUtil.getSqlSession().getMapper(TranDao.class);
    private TranHistoryDao tranHistoryDao = SqlSessionUtil.getSqlSession().getMapper(TranHistoryDao.class);


    @Override
    public boolean save(Clue clue) {
        boolean flag = true;

        int sum = clueDao.save(clue);

        if (sum != 1){
            flag = false;
        }
        return flag;
    }

    @Override
    public Clue detail(String id) {
        Clue clue = clueDao.detail(id);
        return clue;
    }

    @Override
    public boolean unbund(String id) {
        boolean flag = true;

        int count = cardDao.unbund(id);
        if (count != 1){
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean bund(String clueId, String[] activityIds) {
        boolean flag = true;
        for (String aid:activityIds){
            ClueActivityRelation car = new ClueActivityRelation();
            car.setId(UUIDUtil.getUUID());
            car.setClueId(clueId);
            car.setActivityId(aid);
            int count = cardDao.bund(car);
            if (count != 1){
                flag = false;
            }
        }

        return flag;
    }

    @Override
    public boolean conver(String clueId, Tran tran, String createBy) {
        String createTime = DateTimeUtil.getSysTime();

        boolean flag = true;

        //1、根据clueId查询线索
        Clue clue = clueDao.getClueId(clueId);
        //2、获取线索对象的公司属性，这个公司属性就是客户模块的名字，然后根据名字精确匹配，判断客户是否存在
        String company = clue.getCompany();
        Customer cus = customerDao.getCustomerByName(company);

        //判断cus是否为空
        if (cus==null){
            //如果为空，那么客户表中没有这个客户，需要创建一个；
            cus = new Customer();
            cus.setId(UUIDUtil.getUUID());
            cus.setWebsite(clue.getWebsite());
            cus.setPhone(clue.getPhone());
            cus.setAddress(clue.getAddress());
            cus.setOwner(clue.getOwner());
            cus.setNextContactTime(clue.getNextContactTime());
            cus.setName(clue.getCompany());
            cus.setDescription(clue.getDescription());
            cus.setCreateTime(createTime);
            cus.setCreateBy(createBy);
            cus.setContactSummary(clue.getContactSummary());

            //添加客户
            int count1 = customerDao.save(cus);
            if (count1!=1){
                flag = false;
            }
        }

        //3、为联系人模块添加联系人,如果使用到客户id直接用：cus.getId
        Contacts con = new Contacts();
        con.setId(UUIDUtil.getUUID());
        con.setSource(clue.getSource());
        con.setOwner(clue.getOwner());
        con.setAddress(clue.getAddress());
        con.setAppellation(clue.getAppellation());
        con.setContactSummary(clue.getContactSummary());
        con.setNextContactTime(clue.getNextContactTime());
        con.setMphone(clue.getMphone());
        con.setJob(clue.getJob());
        con.setFullname(clue.getFullname());
        con.setEmail(clue.getEmail());
        con.setDescription(clue.getDescription());
        con.setCustomerId(cus.getId());
        con.setCreateTime(createTime);
        con.setCreateBy(createBy);

        //添加联系人
        int count2 = contactsDao.save(con);
        if (count2 != 1){
            flag = false;
        }

        //第三步联系人信息已经拥有了，如果需要联系人id，直接通过:con.getId()获取

        //4、将线索备注信息，转移到客户备注和联系人备注中
        //查询线索备注信息，根据线索id查询
        List<ClueRemark> clueRemarkList = clueRemarkDao.getListByClueId(clueId);

        //遍历每一条备注，将备注添加到客户备注和联系人备注中
        for (ClueRemark clueRemark:clueRemarkList){
            //取出每一条备注的备注信息
            String noteContent = clueRemark.getNoteContent();

            //将备注添加到客户备注
            CustomerRemark customerRemark = new CustomerRemark();
            customerRemark.setId(UUIDUtil.getUUID());
            customerRemark.setCustomerId(cus.getId());
            customerRemark.setNoteContent(noteContent);
            customerRemark.setEditFlag("0");
            customerRemark.setCreateTime(createTime);
            customerRemark.setCreateBy(createBy);

            int count3 = customerRemarkDao.save(customerRemark);
            if (count3 != 1){
                flag = false;
            }



            //将备注添加到联系人备注
            ContactsRemark contactsRemark = new ContactsRemark();
            contactsRemark.setId(UUIDUtil.getUUID());
            contactsRemark.setContactsId(cus.getId());
            contactsRemark.setNoteContent(noteContent);
            contactsRemark.setEditFlag("0");
            contactsRemark.setCreateTime(createTime);
            contactsRemark.setCreateBy(createBy);

            int count4 = contactsRemarkDao.save(contactsRemark);
            if (count4 != 1){
                flag = false;
            }

        }

        //5、转移线索与市场活动关联关系表到联系人与市场活动关联关系表
        //根据clueId获取到相关的市场活动id
        List<ClueActivityRelation> clueActivityRelationList = cardDao.getListByClueId(clueId);
        //遍历关系表,将clueId转移到联系人与市场活动关系表，联系人id就是clueId
        for (ClueActivityRelation clueActivityRelation:clueActivityRelationList){
            //取出每一个市场活动id
            String activityId = clueActivityRelation.getActivityId();

            ContactsActivityRelation contactsActivityRelation = new ContactsActivityRelation();
            contactsActivityRelation.setId(UUIDUtil.getUUID());
            contactsActivityRelation.setActivityId(activityId);
            contactsActivityRelation.setContactsId(con.getId());

            //添加联系人与市场活动关系表
            int count5 = contactsActivityRelationDao.save(contactsActivityRelation);
            if (count5 != 1){
                flag = false;
            }
        }

        //6、如果有交易需求需要创建一笔交易
        if (tran != null){
            //tran不为空创建交易
            /*
            已经赋值：
            tran.setActivityId(activityId);
            tran.setMoney(money);
            tran.setName(name);
            tran.setExpectedDate(expectedDate);
            tran.setStage(stage);
            tran.setId(UUIDUtil.getUUID());
            tran.setCreateBy(createBy);
            tran.setCreateTime(createTime);
             */
            tran.setSource(clue.getSource());
            tran.setOwner(clue.getOwner());
            tran.setNextContactTime(clue.getNextContactTime());
            tran.setDescription(clue.getDescription());
            tran.setCustomerId(cus.getId());
            tran.setContactSummary(clue.getContactSummary());
            tran.setContactsId(con.getId());

            int count6 = tranDao.save(tran);
            if (count6 != 1){
                flag = false;
            }

            //7、如果创建了一笔交易，需要生成交易历史记录
            TranHistory tranHistory = new TranHistory();
            tranHistory.setId(UUIDUtil.getUUID());
            tranHistory.setTranId(tran.getId());
            tranHistory.setExpectedDate(tran.getExpectedDate());
            tranHistory.setStage(tran.getStage());
            tranHistory.setMoney(tran.getMoney());
            tranHistory.setCreateTime(createTime);
            tranHistory.setCreateBy(createBy);

            int count7 = tranHistoryDao.save(tranHistory);
            if (count7 != 1){
                flag = false;
            }

        }

        //8、删除线索备注
        for (ClueRemark clueRemark:clueRemarkList){
            int count8 = clueRemarkDao.delete(clueRemark);
            if (count8 != 1){
               flag = false;
            }
        }

        //9、删除线索与市场活动关系
        for (ClueActivityRelation clueActivityRelation:clueActivityRelationList){
            int count9 = cardDao.delete(clueActivityRelation);
            if (count9 != 1){
                flag = false;
            }
        }

        //10、删除市场活动
        int count10 = clueDao.delete(clueId);
        if (count10 != 1){
            flag = false;
        }



        return flag;

    }


}
